<?php

namespace app\common\model;

use app\common\model\Common;
use think\model\concern\SoftDelete;
class MaterialType extends Common
{
    use SoftDelete;
    protected $deleteTime = 'deleted_at';
    protected  $table = 'material_type';

     // 查询
     public function getMaterial($pid = 0){

        $field = 'id,name,parent_id';
        $sort = 'sort '.$this->sysconfig['sort'];
        $where = [];
        $where[] = ['parent_id','=',$pid];
       
   
        $materialList = $this->getAllOrder($where,$field,$sort)->toArray();
        // 判断是否有子集
        foreach($materialList as &$v){
            $wherechild = ['parent_id'=>$pid];
            $has = $this->countRows($wherechild);
            if($has){
                $v['child'] = $this->getMaterial($v['id']);
            }
        }
        return $materialList;
    }
    // 删除菜单
    public function destroyMaterial($id){
        MaterialType::destroy($id);
        $haschild = $this->countRows(['parent_id'=>$id]);
        if($haschild){
            $menu = $this->getAll(['parent_id'=>$id],'id,parent_id');
            foreach($menu as $v){
                $this->destroyMaterial($v['id']);
            }
        }
    }
    
}